package com.zhanghp.class015;

import java.util.Stack;

/**
 * @author zhanghp
 * @since 2024/3/4 14:09
 */
// 测试链接 : https://leetcode.cn/problems/min-stack/
public class GetMinStack {

    static class Stack1{
        public Stack<Integer> stack = new Stack<>();
        public Stack<Integer> min = new Stack<>();

        public void push(int val) {
            stack.push(val);
            if (min.isEmpty() || min.peek() > val) {
                min.push(val);
            }else {
                min.push(min.peek());
            }
        }

        public void pop() {
            stack.pop();
            min.pop();
        }

        public int top() {
            return stack.peek();
        }

        public int getMin() {
            return min.peek();
        }
    }

    static class Stack2{
        // leetcode的数据在测试时，同时在栈里的数据不超过这个值
        // 这是几次提交实验出来的，哈哈
        // 如果leetcode补测试数据了，超过这个量导致出错，就调大
        public final int MAX_TIME = 8001;
        public int[] stack;
        public int[] min;
        public int size;
        public Stack2(){
            stack  = new int[MAX_TIME];
            min  = new int[MAX_TIME];
            size = 0;
        }
        public void push(int val) {
            stack[size] = val;
            if (size == 0 || min[size - 1] > val) {
                min[size] = val;
            }else {
                min[size] = min[size - 1];
            }
            size ++;
        }

        public void pop() {
            size --;
        }

        public int top() {
            return stack[size - 1];
        }

        public int getMin() {
            return min[size - 1];
        }
    }

}
